//
//  QActiveWebViewVC.m
//  QoilPublic
//
//  Created by zhuang on 16/12/7.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QActiveWebViewVC.h"
#import "QTopupMoneyVC.h"
#import "QSubmitOrderModel.h"
#import "QConfirmOrderInforMationVC.h"

@interface QActiveWebViewVC ()

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation QActiveWebViewVC

- (void)dealloc{

    CLog(@"销毁了");
}

- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self.view addSubview:self.webView];

    [self requestData];
}

- (void)requestData{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [_webView loadRequest:request];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [QViewUtility hideMBHudToView:self.view];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = // 打印异常
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        CLog(@"%@", exceptionValue);
    };
    
    //[self.context evaluateScript:[QUserInfo sharedInfo].loginModel.token];
    
    __weak QActiveWebViewVC *wSelf = self;
    self.context[@"recharge"] =
    ^(){
        __strong QActiveWebViewVC *sSelf = wSelf;
        CLog(@"----");
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_sync(mainQueue, ^{
            QTopupMoneyVC *vc = [[QTopupMoneyVC alloc] init];
            [sSelf.navigationController pushViewController:vc animated:YES];
        });
    };
    
    self.context[@"getToken"] =
    ^(){
        CLog(@"完成===%@",[QUserInfo sharedInfo].loginModel.token);
        return [QUserInfo sharedInfo].loginModel.token;
    };
    
    self.context[@"receivequest"] =
    ^(id request){
        __strong QActiveWebViewVC *sSelf = wSelf;
       NSDictionary *dic = (NSDictionary *)request;
        QBaseModel *basModel = [QBaseModel objectFromDict:dic];
        if([basModel.result isEqualToString:@"success"]){
            QSubmitOrderModel *model = [QSubmitOrderModel objectFromDict:basModel.items];
            QConfirmOrderInforMationVC *vc = [[QConfirmOrderInforMationVC alloc] init];
            vc.model = model;
            [sSelf.navigationController pushViewController:vc animated:YES];
        }else{
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    };
}

#pragma mark - Getter/Setter
- (UIWebView*)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 96, kScreenWidth, kScreenHeight)];
//        _webView.frame = CGRectMake(0, 96, kScreenWidth, kScreenHeight);
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
    }
    return _webView;
}

@end
